package com.itheima.mp.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

@ApiOperation("新增用户接口")
@PostMapping
public void savaUser(@RequestBody UserFormDTO userDTO){
    //1.负责dto 到 user
    User user = BeanUtil.copyProperties(userDTO, User.class);
    userService.save(user);
}
    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }
    //改造
    /*
     需求：改造根据id用户查询的接口，查询用户的同时返回用户收货地址列表
     */
    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id){
    //基于自定义serice查询
        return userService.queryUserAndAddressById(id);
    }
    @ApiOperation("根据ids 批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserById(@ApiParam("用户集合ids") @RequestParam("ids") List<Long> ids){
        /*//1.查询用户po
        List<User> users = userService.listByIds(ids);
        //2.把po拷贝到vo
        return BeanUtil.copyToList(users, UserVO.class);*/
        return userService.queryListUserAndAddressById(ids);
    }
    @ApiOperation("扣减用户余额接口")
    @DeleteMapping("/{id}/deduction/{money}")
    public void deductBalance(@ApiParam("用户id") @PathVariable("id") Long id,@ApiParam("扣减的余额") @PathVariable("money") Integer money){
       userService.deductBalance(id,money);
    }
    @ApiOperation("根据复杂查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery userQuery){
        //1.查询用户po
        List<User> users = userService.queryUsers(userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        return BeanUtil.copyToList(users, UserVO.class);
    }
    @ApiOperation("根据用户查询收获地址接口")
    @GetMapping("/addresss/{id}")//拦截到Id
    public List<AddressVO> queryAddressById(@ApiParam("用户id") @PathVariable("id") Long id){
        //基于自定义serice查询
        return userService.queryUserAddressById(id);
    }
    @ApiOperation("根据复杂分页查询用户接口")
    @GetMapping("/page")
    public PageDTO<UserVO> queryPageUsers(UserQuery userQuery){
        //1.查询用户po
     return userService.queryPageUsers(userQuery);
    }

}
